#include "EventLoop.hpp"
#include "TcpServer.hpp"

#include <arpa/inet.h>
#include <iostream>

int main() {
    EventLoop loop;

    sockaddr_in listenAddr{};
    listenAddr.sin_family = AF_INET;
    listenAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    listenAddr.sin_port = htons(8888);

    TcpServer server(&loop, listenAddr);
    server.setThreadNum(5);  // 使用 4 个子线程处理连接

    server.setConnectionCallback([](const std::shared_ptr<TcpConnection>& conn) {
        std::cout << ">> connection established, fd = " << conn->fd() << std::endl;
    });

    server.setMessageCallback([](const std::shared_ptr<TcpConnection>& conn, std::string& msg) {
        std::cout << ">> recv: " << msg;
        conn->send(msg);
    });

    server.start();
    loop.loop();

    return 0;
}
